package com.example.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author weiyx
 * @date 2021-01-10
 */
public class producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        //获取连接
        Connection connection = connectionFactory.newConnection();
        //通过connection创建一个channel
        Channel channel = connection.createChannel();

        String exchangeName = "exchange001";
        String routingKey = "routingKey0011";

        //发布消息
        for(int i = 0; i < 5; i++){
            String msg = "hello rabbitmq " + i;
            //1.exchange  2.routing key
            //默认交换器隐式绑定到每个队列，其路由键等于队列名. 如果没有队列名,则会被删除
            channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
        }
        channel.close();
        connection.close();

    }
}
